#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1005;

int n;
map<int, int> teamid;  // 队员团队ID
int q[N], head, tail;  // 队列中团队顺序
int pos[N];            // 团队位置
list<int> lst[N];      // 同一团队中，队员顺序

void init() {
  head = tail = 1;
  teamid.clear();
  rep(i, 1, n) pos[i] = 0, lst[i].clear();
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int id = 0;
  while (cin >> n && n) {
    ++id;
    cout << "Scenario #" << id << endl;
    init();
    rep(i, 1, n) {
      int len;
      cin >> len;
      rep(j, 1, len) {
        int x;
        cin >> x;
        teamid[x] = i;
      }
    }
    string cmd;
    while (cin >> cmd && cmd != "STOP") {
      if (cmd == "ENQUEUE") {
        int x;
        cin >> x;
        int tid = teamid[x];
        if (!pos[tid]) q[tail] = tid, pos[tid] = tail++;  // 团队入队
        lst[tid].push_back(x);                            // 队员入队
      } else {
        int f = q[head];
        int x = lst[f].front();
        cout << x << endl;
        lst[f].pop_front();                      // 队员出队
        if (lst[f].empty()) head++, pos[f] = 0;  // 团队出队
      }
    }
    cout << endl;
  }
  return 0;
}